//jQ start
$(function () {
//    获取组织列表
    let org_arr = []
    function get_org_list() {
        //ajax start
        $.ajax({
            url: baseURL + '/auth/api/org/getOrgList',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {},
            success: function (e) {
                console.log(e);
                //for i 循环 start

                for (let i = 0; i < e.content.length; i++) {
                    $("#org_list").append(
                        '<option id=' + e.content[i].uuid + '>' + e.content[i].orgname + '</option>'
                    );
                    let arr = {};
                    arr.uuid = e.content[i].uuid;
                    arr.orgname = e.content[i].orgname;
                    arr.pid = e.content[i].pid;
                    org_arr = org_arr.concat(arr)
                }
                console.log(org_arr);
            }

        })
    }

    get_org_list()


//    根据输入的姓名获取人员id
    function get_user_id() {
        let user_name = $("#org_user_name").val()
        //ajax start
        $.ajax({
            url: baseURL + '/auth/api/user/searchUserList',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                username: user_name,
            },
            success: function (e) {
                let uid = e.content[0].uid
                $("#org_user_uid").val(uid);
                return uid
            }

        })
    }

    $("#org_user_name").on('change', function () {
        get_user_id()
    });

//    添加人员到组织部分
    function add_user_to_org() {
        let user_id = $("#org_user_uid").val();
        let org_uid = $("#org_list").find("option:checked").attr('id');
        console.log(org_uid);
        //ajax start
        $.ajax({
            url: baseURL + '/auth/api/orgUser/addUserToOrg',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                userIds: user_id,
                orgId: org_uid,
            },
            success: function (e) {
                console.log(e);
                //if 判断 start
                if (e.code == 1) {
                    alert("添加成功", window.location.reload())
                } else if (e.code != 1) {
                    alert("添加失败 code:", e.code)
                }
            }

        })
    }

    $("#save_org_user_content").click(function () {
        add_user_to_org()
    });

//    组织关系侧栏部分

//    查询组织成员及分页

    function show_data_org_admin_user() {
        //ajax start
        $.ajax({
            url: baseURL + '/auth/api/orgUser/getSysUserList',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                orgId: 'f60dfee2-67fa-477f-a309-779e72c91e0c',
                pageNum: 1,
                pageSize: 10,
            },
            success: function (e) {
                console.log(e);
            }

        })
    }

    show_data_org_admin_user()


})